﻿using System.Globalization;
using System.Windows.Controls;

namespace LFSRecord2.View.Validators
{
    public class InSimPortValidationRule : ValidationRule
    {
        public int MinPort { get; set; }
        public int MaxPort { get; set; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            int port;

            // Is port a whole number?
            if (!int.TryParse((string)value, out port))
            {
                return new ValidationResult(false, "Not a whole number.");
            }

            // Is in range?
            if ((port < MinPort) || (port > MaxPort))
            {
                string msg = string.Format(CultureInfo.InvariantCulture, "Port must be between {0} and {1}.", MinPort, MaxPort);
                return new ValidationResult(false, msg);
            }

            // Valid port
            return new ValidationResult(true, null);
        }
    }
}
